package com.dotstone.chipism.ui.security.me;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.dotstone.chipism.R;
import com.dotstone.chipism.activity.BaseActivity;
import com.dotstone.chipism.view.slidingfragmenu.BarUtils;
import com.tbruyelle.rxpermissions2.Permission;
import com.tbruyelle.rxpermissions2.RxPermissions;

import io.reactivex.functions.Consumer;

public class RouterSetupActivity extends BaseActivity {

    private LinearLayout mBackL;
    private TextView tv_statusBar_mian;
    private EditText et_wifi_password;
    private Button btn_confirm;
    private TextView tv_wifi_ssid;


    @Override
    public void widgetClick(View v) {
        switch (v.getId()){
            //返回
            case R.id.back_layout:
                finish();
                break;
            //确定
            case R.id.btn_confirm:
                final String pwd = et_wifi_password.getText().toString();
                if (TextUtils.isEmpty(pwd)){
                    showToast(getString(R.string.pwd_not_null));
                    return;
                }
                new RxPermissions(this).requestEach(Manifest.permission.ACCESS_FINE_LOCATION).subscribe(new Consumer<Permission>() {
                    @Override
                    public void accept(Permission permission) throws Exception {
                        if (permission.granted){
                            //用户同意
                            if(isOPen(getApplicationContext())){
                                Intent intent = new Intent(getApplicationContext() , CameraConnectActivity.class);
                                intent.putExtra("password" , pwd);
                                startActivity(intent);

                            } else {
                                showToast("请打开GPS");
                            }
                        }else if (permission.shouldShowRequestPermissionRationale){
                            //用户取消
                        }else {
                            //取消并不在询问
                            showToast("请在权限设置中打开位置权限");
                        }
                    }
                });

                break;
        }
    }

    @Override
    public void initParms(Bundle parms) {

    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_router_setup;
    }

    @Override
    public void initView(View view) {
        mBackL = $(R.id.back_layout);
        tv_statusBar_mian = $(R.id.tv_statusBar_mian);
        et_wifi_password = $(R.id.et_wifi_password);
        btn_confirm = $(R.id.btn_confirm);
        tv_wifi_ssid = $(R.id.tv_wifi_ssid);
    }

    @Override
    public void setListener() {
        mBackL.setOnClickListener(this);
        btn_confirm.setOnClickListener(this);
        boolVersion();
    }

    @Override
    public void doBusiness(Context mContext) {
        new RxPermissions(this).requestEach(Manifest.permission.ACCESS_FINE_LOCATION).subscribe(new Consumer<Permission>() {
            @Override
            public void accept(Permission permission) throws Exception {
                if (permission.granted){
                    //用户同意
                    tv_wifi_ssid.setText(getConnectWifiSSID());
                }else if (permission.shouldShowRequestPermissionRationale){
                    //用户取消
                }else {
                    //取消并不在询问
                }
            }
        });
    }


    /**
     * 判断GPS是否开启，GPS或者AGPS开启一个就认为是开启的
     * @param context
     * @return true 表示开启
     */
    public final boolean isOPen(final Context context) {
        LocationManager locationManager
                = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        // 通过GPS卫星定位，定位级别可以精确到街（通过24颗卫星定位，在室外和空旷的地方定位准确、速度快）
        boolean gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        // 通过WLAN或移动网络(3G/2G)确定的位置（也称作AGPS，辅助GPS定位。主要用于在室内或遮盖物（建筑群或茂密的深林等）密集的地方定位）
        boolean network = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        if (gps || network) {
            return true;
        }

        return false;
    }

        @Override
    protected void onResume() {
        super.onResume();
        tv_wifi_ssid.setText(getConnectWifiSSID());
    }

    private String getConnectWifiSSID() {
        try {
            WifiManager wifimanage=(WifiManager)getApplicationContext().getSystemService(Context.WIFI_SERVICE);
            return wifimanage.getConnectionInfo().getSSID().replace("\"", "");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public void boolVersion() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

            Window window = getWindow();
            window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            // 透明导航栏
//			getWindow().addFlags(
//					WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            // 改变titlebar的高度
            int statusbarHeight = BarUtils.getStatusBarHeight(this);
            RelativeLayout.LayoutParams layoutParams1 = (RelativeLayout.LayoutParams) tv_statusBar_mian
                    .getLayoutParams();
            layoutParams1.height = statusbarHeight;
            tv_statusBar_mian.setLayoutParams(layoutParams1);
            tv_statusBar_mian.setVisibility(View.VISIBLE);
        } else {
            tv_statusBar_mian.setVisibility(View.GONE);
        }
    }
}
